home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 10 / Commodore_Free_Issue_10_2007_Commodore_Computer_Club.d64 / t.slang tut2 < prev   
Text File  |  2023-02-26  |  11KB  |  407 lines

  1. u        SLANG TUTORIAL PART 2
  2.  
  3. Tutorial lesson #9:
  4. What we are going to use is a simple
  5. source code file. Try typing in the
  6. following code, and compile and run
  7. it:
  8.  
  9. byte b for b=1:10 println "b=" b next
  10. print "press any key..." waitchar
  11. done
  12.  
  13. put 'putcore.e.s'
  14.  
  15. The two critical differences here are
  16. 1) we are now using print instead of
  17. sprint, and 2) the "put" command at
  18. the end of the file.
  19.  
  20. For now, instead of going into detail
  21. on PUT, let's just say that you need
  22. that line at the DendD of your code
  23. -- after the done statement -- if you
  24. want to use the full print command.
  25.  
  26. Notice that we have put both a string
  27. and a number on the print line,
  28. similar to BASIC. You can also
  29. separate these with commas, if you
  30. like:
  31.  
  32. print "b=",b
  33.  
  34. It just depends on which way you
  35. think is clearer. You can also print
  36. to any part of the screen:
  37.  
  38. print(b,20) "this is row "b
  39.  
  40. Tutorial lesson #10:
  41. Replace the println statment with the
  42. above "this is row" statement, and see
  43. what happens.
  44.  
  45. As you can see, print is much more
  46. powerful and flexible than sprint, but
  47. the downside is that you need to PUT
  48. the core library in there, which
  49. increases compile time and makes the
  50. program much larger. Experience will
  51. help you figure out when you want to
  52. use one or the other.
  53.  
  54. The core library Above we used the
  55. core library, using the line put
  56. "putcore.e.s" at the end of the
  57. program (whether you use ' or " quotes
  58. doesn't matter, incidentally). This is
  59. a very important library and is needed
  60. for more than just print.
  61.  
  62. Tutorial lesson #11:
  63. Multiplication and division of
  64. bytes/ints requires this library, and
  65. you will get an error if you don't
  66. include the core library. (Print will
  67. also generate an error.) Try the
  68. following program:
  69.  
  70. int b b=10 b=b*2 done
  71.  
  72. Now compile it, and you will get an
  73. error at the multiplication. Then add
  74. the line
  75.  
  76. put 'putcore.e.s'
  77.  
  78. to the end of the program, compile...
  79. and it will work. And since you're
  80. including the core library anyways,
  81. you can go ahead and print out b if
  82. you want to.
  83.  
  84. Why use a library? If you know BASIC,
  85. you know that a command like "print"
  86. actually calls a routine in the BASIC
  87. ROMs to do its thing. The BASIC ROMs
  88. are, for the most part, one big
  89. library, but they are in ROM instead
  90. of in a disk file.
  91.  
  92. Sometimes machine language programmers
  93. will call these BASIC routines instead
  94. of writing their own. Similarly, in a
  95. compiled language like Slang,
  96. sometimes it makes sense to call a
  97. common routine to perform some task. A
  98. library is nothing more than a
  99. collection of useful routines.
  100.  
  101. Arrays and Strings We're just about
  102. done here. I'm not going to cover
  103. DallD of the available commands; the
  104. goal here is to get across the major
  105. commands, and then youcan browse the
  106. slangref.txt document to check out
  107. other commands. So I'll just touch on
  108. the remaining topics at this point.
  109. Arrays in Slang work much the same as
  110. arrays in BASIC. You declare them very
  111. similarly to other variables:
  112.  
  113. int b ;regular variable int c(20)
  114. ;array
  115.  
  116. This is similar to using the DIM
  117. statement, if that helps. There's just
  118. one thing to remember: array indices
  119. start at 0. In the above declaration,
  120. you can address c(0), c(1), c(2), ...
  121. c(19), like
  122.  
  123. c(19) = 1000
  124.  
  125. but
  126.  
  127. c(20) = 1000
  128.  
  129. will be incorrect (and may cause a
  130. crash). There are 20 elements total: 0
  131. through 19. If it's too confusing,
  132. then one thing you can do is to add
  133. one extra element to any array --
  134. like, use "int c(21)" -- and not worry
  135. about it.
  136.  
  137. Strings also are really similar to
  138. BASIC, but you're going to have to get
  139. one thing straight in your head:
  140. strings are really just bytes. There
  141. is no "string" type, like
  142. byte/int/float; strings are just
  143. bytes.
  144.  
  145. You already know this, from BASIC:
  146.  
  147. d$="a" ;treating letter "a" as a
  148. string d$=chr$(65) ;treating "a" as
  149. the number 65
  150.  
  151. The letter "a" is really just a number
  152. -- 65 in this case. This works the
  153. same way in Slang:
  154.  
  155. ubyte d d = "a" ;a "string" d = 65 ;a
  156. number
  157.  
  158. That's fine for just one character,
  159. but that's not a string. In Slang, a
  160. string is just a byte array:
  161.  
  162. ubyte d(20)
  163.  
  164. d$ = "hello" ;treat as a string d(0)
  165. = "y" ;change string to "yello" d(0)
  166. = 67 ;change string to "cello"
  167.  
  168. In this example, d is a byte array.
  169. You can treat it as a string, or as a
  170. list of numbers. As in BASIC, the "$"
  171. tells Slang to treat the variable as a
  172. string -- I won't go into the details
  173. about this, but I think the meaning is
  174. pretty clear from the above example.
  175.  
  176. Tutorial lesson #12
  177. Here's a simple program using strings
  178. to try out:
  179.  
  180. ubyte d(20),i d$="hello" println
  181. "d$=" d$ d(0) = 67 println "d$=" d$
  182. for i=0:5
  183.  println d(i) endfor waitchar done
  184.  
  185. Go ahead and play around with it --
  186. try adding 1 to each element of the
  187. string, etc. One important thing to
  188. note: when you run the program, you'll
  189. notice that the last number printed in
  190. the for-loop is a 00. With strings,
  191. the very last element will be zero --
  192. this is what tells Slang where the end
  193. of the string is. If you overwrite
  194. this ending zero, you'll generally get
  195. a whole bunch of garbage. So.. .
  196.  
  197. Tutorial lesson #13
  198. Change the line
  199.  
  200. d(0) = 67
  201.  
  202. to
  203.  
  204. d(5) = 67
  205.  
  206. and see what happens when the string
  207. is printed. This overwrites the ending
  208. zero byte, and should in general print
  209. out a bunch of garbage. Sometimes, if
  210. you're lucky, there will be another 00
  211. somewhere in the array!
  212.  
  213. If-elseif-endif The if-then
  214. structure:
  215.  
  216. if a=10 do something elseif a=11 do
  217. something else endif
  218.  
  219. It's similar to the BASIC command,
  220. except that you have an elseif or
  221. endif command to mark the block of
  222. code to be executed, just like while
  223. and repeat.
  224.  
  225. Subroutines In BASIC, you are familiar
  226. with GOSUB -- it calls a subroutine,
  227. and then RETURN returns back to the
  228. place of the subroutine call. Slang
  229. subroutines are more sophisticated,
  230. but the idea is the same and overall
  231. they are pretty similar.
  232.  
  233. A subroutine is like a mini-program:
  234. you can define variables, have a bunch
  235. of statements, etc. The neat thing is
  236. that these variables and statements
  237. are DlocalD to the subroutine -- they
  238. "belong" to the subroutine, and do not
  239. interact with other subroutines. This
  240. allows you to organize your programs
  241. efficiently.
  242.  
  243. Just like a variable, you have to
  244. define a subroutine. And you need to
  245. end the routine using "endsub" .
  246.  
  247. With a subroutine, you can pass
  248. DparametersD to the routine. For
  249. example, the graphics library has a
  250. routine called GrPlot, to plot a
  251. point. And it requires two parameters:
  252. the x- and y-position of the point to
  253. be plotted:
  254.  
  255. GrPlot(x1,y1)
  256.  
  257. Subroutines can also pass parameters
  258. DbackD to the calling routine. Once
  259. you "get" the idea of parameter
  260. passing, and of local variables, the
  261. rest is a cinch.
  262.  
  263. Tutorial lesson #14:
  264. Type in and run the following program,
  265. which demonstrates the idea of local
  266. variables within a subroutine:
  267.  
  268. byte b b=1 TestRoutine() ;Call the
  269. subroutine println "but in the main
  270. code, b is still "b waitchar done
  271.  
  272. sub TestRoutine() ;Define the
  273. subroutine
  274.  
  275. byte b ;create the DlocalD variable b
  276. b=10
  277.  println "in the subroutine, b="b
  278.  
  279. endsub ;end subroutine
  280.  
  281. put "putcore.e.s"
  282.  
  283. You can see what I mean about a
  284. subroutine being a mini-program.
  285. Within the subroutine you declare
  286. variables, have statements, and end it
  287. with an endsub. You don't really worry
  288. about what other routines or the main
  289. program does; the subroutine doesn't
  290. "see" them.
  291.  
  292. So, a few observations: the "sub"
  293. keyword defines a subroutine. We
  294. DcallD the subroutine by simply typing
  295. the subroutine name in the main
  296. program. Once the routine finishes,
  297. the calling program continues
  298. executing where it left off.
  299.  
  300. The subroutine declares its own
  301. variable b. Even though it has the
  302. same name as the variable in the main
  303. program, this is a DlocalD variable;
  304. it "belongs" to the subroutine, not to
  305. the main program.
  306.  
  307. Tutorial lesson #15
  308. In this lesson, we'll try parameter
  309. passing.
  310.  
  311. int x1,y1 x1=12 y1=20 AddEm(x1,y1)
  312. println "the result was "
  313. AddEm<-result waitchar done
  314.  
  315. sub AddEm(int a, int b)<-int result
  316. result = a+b endsub
  317.  
  318. put "putcore.e.s"
  319.  
  320. (As before, <- is the backarrow key)
  321.  
  322. In this example, the subroutine AddEm
  323. takes two DinputD parameters, a and b,
  324. and has one DoutputD parameter,
  325. result. As before, all three of
  326. theseparameters are local to the
  327. subroutine, so they could have the
  328. same name as variables in the main
  329. program.
  330.  
  331. With the return parameter, what we are
  332. really doing is making a subroutine
  333. variable DvisibleD to outside
  334. routines. Normally all variables and
  335. such inside a subroutine belong to the
  336. subroutine, and are not available
  337. outside of the routine; this is how to
  338. make specific ones available outside
  339. of the subroutine. Just as with input
  340. variables, there can be a whole list
  341. of output variables.
  342.  
  343. Back in the main program, this is just
  344. another variable called AddEm<-result.
  345. You can use it in expressions, etc.
  346. just like any other variable:
  347.  
  348. x1 = x1 + 2*AddEm<-result
  349.  
  350. So in summary: a subroutine is like a
  351. mini-program, with its own variables
  352. and statements. But, you can pass
  353. parameters into the subroutine, and
  354. youcan retrieve variables out of the
  355. subroutine, as needed. Subroutines are
  356. really helpful in organizing and
  357. simplifying programs, so it's well
  358. worth taking the time to understand
  359. them if you don't already!
  360.  
  361. Saving object code Once you've written
  362. a program, what do you do with it?
  363. What we're going to do here is save
  364. the DobjectD code, and then load and
  365. run it.
  366.  
  367. Tutorial lesson #16
  368. (always need some multiple of 16,
  369. right? )
  370.  
  371. Compile one of the example programs
  372. above (one that works!). Once it
  373. compiles successfully, press F7 to
  374. enter the disk menu. First, save the
  375. source code by pressing "s". Go ahead
  376. and enter some filename; the program
  377. will automatically append a ".s" to
  378. the filename.
  379.  
  380. Second, save the object code by
  381. pressing "o". You'll see the same
  382. filename as default, so go ahead and
  383. press return. If you now list the
  384. directory, you should see two new
  385. files: one with a .s, the other with a
  386. .o. You can now load and run the
  387. object code totally indepently from
  388. Slang.
  389.  
  390. Exit back to the editor. We are now
  391. going to exit to BASIC: press
  392. shift-ctrl- < (backarrow, to the left
  393. of the '1' key), and you should be
  394. back at the BASIC prompt. If you'd
  395. like to try your program from here,
  396. you can type "sys 4096" .
  397.  
  398. Now reset the machine. Go to the disk
  399. directory, and load your object file
  400. ,8,1.
  401.  
  402. Your object code is a machine language
  403. file, just like other programs you may
  404. own or download. Once it's loaded,
  405. type "sys 4096" to run it. (By
  406. default, Slang programs are located at
  407. 4096, but it is easy to loca
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661. G